<?php

namespace app\aiv110\controller;

use app\aiv110\controller\CommonController;
use think\Validate;
use think\Db;
use app\util\Utils;
use app\aiv110\model\SpecialQrcode;
use app\aiv110\model\Custom;
use app\aiv110\model\CtmSpecial;
use app\aiv110\model\Redpacket;
use app\aiv110\model\Medical;
use app\aiv110\model\MedicalAddress;

class SpecialController extends CommonController {
    /*
     * code列表
     */

    public function code() {
        /*
         * 0  成功
         * 1  参数错误
         * 2  验证失败
         * 40000  码不可重复使用
         * 40001  您已领取过，不可再次领取
         * 40002  兑换失败
         * 40003  请上传工作证
         * 40004  您已是特权用户
         * 40005  您已是体验特权，已免费为您升级为正式特权
         * 40006  开通失败
         * 40007  您已经申请过了
         * 40008  提交失败
         * 40009  图片最多六张
         * 40010  逆行者申请活动已截止，请继续关注平台其他活动
         */
    }

    /*
     * 扫码领取红包兑换特权
     * 日期 2019-09-29
     */

    public function specialAndRed() {
        $rule = [
            'custom_phone' => 'require|length:11',
            'num' => 'require|length:6',
        ];
        $msg = [
            'custom_phone.require' => '手机号必须',
            'custom_phone.length' => '手机号长度为11',
            'num.require' => '请输入兑换码',
            'num.length' => '兑换码长度为6',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }

        $custom_phone = $all['custom_phone'];
        $num = $all['num'];

        $specialdata = SpecialQrcode::where('num', '=', $num)->find();
        if ($specialdata['phone'] != 0) {
            return Utils::ajaxReturn(40000, '码不可重复使用', null);
        }
        $specialdata1 = SpecialQrcode::where('phone', '=', $custom_phone)->count();
        if ($specialdata1>=3) {
            return Utils::ajaxReturn(40001, '您已领取过，不可再次领取', null);
        }

        Db::startTrans();

        $ctmRow = Custom::where('custom_phone', $custom_phone)->find();
        $ctm_id = $ctmRow['id'];
        //领红包
        $shareResult = Custom::where('id', '=', $ctm_id)->setInc('red_money', $specialdata['red_money']);
        $redData['ctm_id'] = $ctm_id;
        $redData['money'] = $specialdata['red_money'];
        $redData['type'] = 1;
        $redData['add_time'] = date('Y-m-d H:i:s', time());
        $ctmRedPacket = Redpacket::insert($redData);
        //改特权用户
//        if ($ctmRow['vip_status'] == 2) {
//            $custom_money_result = true;
//            //修改客户特权卡
//            $ctm_vip_result = CtmSpecial::where('ctm_id', $ctm_id)->update([
//                'end_time' => date('Y-m-d H:i:s', time() + 86400 * 365),
//            ]);
//        } else {
//            $custom_money_result = Custom::where('id', '=', $ctm_id)->update([
//                'vip_status' => 2,
//            ]);
//            //写入或客户特权卡
//            $ctm_vip_result = CtmSpecial::insert([
//                        'ctm_id' => $ctm_id,
//                        'end_time' => date('Y-m-d H:i:s', time() + 86400 * 365),
//                        'add_time' => date('Y-m-d H:i:s'),
//            ]);
//        }
        //修改兑换
        $savespecial = SpecialQrcode::where('num', '=', $num)
                ->update(['phone' => $custom_phone, 'get_time' => date('Y-m-d H:i:s')]);
        if ($shareResult && $savespecial && $ctmRedPacket) {
            Db::commit();
            return Utils::ajaxReturn(0, '兑换成功', $specialdata['red_money']);
        } else {
            Db::rollback();
            return Utils::ajaxReturn(40002, '兑换失败', null);
        }
    }
    
    /*
     * 白衣天使免费领取特权卡
     * 日期 2020-02-02
     */

    public function medicalGetSpecial() {
        $rule = [
            'ctm_id' => 'require',
            'name' => 'require',
            'work_unit' => 'require',
            'job' => 'require',
        ];
        $msg = [
            'ctm_id.require' => '用户ID必须',
            'name.require' => '姓名必须',
            'work_unit.require' => '工作单位必须',
            'job.require' => '职务必须',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }

        $ctm_id = $all['ctm_id'];
        $name = $all['name'];
        $work_unit = $all['work_unit'];
        $job = $all['job'];
        $job_img = input('param.job_img'); //工作证
        $ctmRow = Custom::where('id', $ctm_id)->find();
        if($ctmRow['vip_status'] == 2){
            return Utils::ajaxReturn(40004, '您已是特权用户', null);
        }
        if (empty($job_img)) {
            return Utils::ajaxReturn(40003, '请上传工作证', null);
        }  else {//上传图片
            $IndexPath = config('appconfig.IndexPath');
            $postFields['modeName'] = 'staff';
            $postFields['img'] = $job_img;
            $img = Utils::curlPost1($IndexPath . 'Public/uploadBase64Img/', $postFields);
            $img = json_decode($img,TRUE);
            $job_img = $img['data'];
        }
        Db::startTrans();
        
        $insertMedical = Medical::insert([
            'ctm_id' => $ctm_id,
            'name' => $name,
            'work_unit' => $work_unit,
            'job' => $job,
            'job_img' => $job_img,
            'add_time' => date('Y-m-d H:i:s'),
        ]);
        
        //改特权用户
        if ($ctmRow['vip_status'] == 3) {
            $custom_money_result = Custom::where('id', '=', $ctm_id)->update([
                'vip_status' => 2,
            ]);
            //修改客户特权卡
            $ctm_vip_result = CtmSpecial::where('ctm_id', $ctm_id)
                    ->where('type',2)
                    ->order('id desc')
                    ->update([
                'end_time' => date('Y-m-d H:i:s', time() + 86400 * 395),
                'type' => 3,
            ]);
            $code = '40005';
            $note = '您已是体验特权，已免费为您升级为正式特权';
        }else {
            $custom_money_result = Custom::where('id', '=', $ctm_id)->update([
                'vip_status' => 2,
            ]);
            //写入或客户特权卡
            $ctm_vip_result = CtmSpecial::insert([
                        'ctm_id' => $ctm_id,
                        'end_time' => date('Y-m-d H:i:s', time() + 86400 * 395),
                        'add_time' => date('Y-m-d H:i:s'),
                        'type' => 3,
            ]);
            $code = '0';
            $note = '开通成功';
        }
        
        if ($insertMedical && $custom_money_result && $ctm_vip_result) {
            Db::commit();
            return Utils::ajaxReturn($code, $note, null);
        } else {
            Db::rollback();
            return Utils::ajaxReturn(40006, '开通失败', null);
        }  
    }
    
    /*
     * 白衣天使报名
     * 日期 2020-02-17
     */

    public function medicalEnroll() {
        $rule = [
            'ctm_id' => 'require',
            'name' => 'require',
            'work_unit' => 'require',
            'job' => 'require',
            'phone'=> 'require',
            'currentcity' => 'require',
            'status' => 'require',
            'address' => 'require',
            'invite_id' => 'require',
        ];
        $msg = [
            'ctm_id.require' => '用户ID必须',
            'name.require' => '姓名必须',
            'work_unit.require' => '工作单位必须',
            'job.require' => '职务必须',
            'phone.require' => '手机号必须',
            'currentcity.require' => '所在城市必须',
            'status.require' => '是否进店必须',
            'address.require' => '邮寄地址必须',
            'invite_id.require' => '邀请者id必须',
            
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }
        return Utils::ajaxReturn(40010, '逆行者申请活动已截止，请继续关注平台其他活动', null);
        $ctm_id = $all['ctm_id'];
        $name = $all['name'];
        $work_unit = $all['work_unit'];
        $job = $all['job'];
        $invite_id = $all['invite_id'];
        $phone = $all['phone'];
        $currentcity = $all['currentcity'];
        $status = $all['status'];
        $address = $all['address'];
        $job_img = input('param.job_img/a'); //	工作场景照
        $job_imgarray = $job_img;
        $papers_img = input('param.papers_img/a'); //证件照
        $papers_imgarray = $papers_img;
        $card_img = input('param.card_img/a'); //手持身份证
        $card_imgarray = $card_img;
        //$job_imgarray = explode(",", $job_img);
        $ctmRow = Custom::where('id', $ctm_id)->find();
//        if($ctmRow['vip_status'] == 2){
//            return Utils::ajaxReturn(40004, '您已是特权用户', null);
//        }
        if(count($job_imgarray)>6){
            return Utils::ajaxReturn(40009, '图片最多六张', null);
        }
        if(count($papers_imgarray)>2){
            return Utils::ajaxReturn(40009, '证件照最多俩张', null);
        }
        if(count($card_imgarray)>1){
            return Utils::ajaxReturn(40009, '手持身份证最多一张', null);
        }
        $MedicalRow = Medical::where('ctm_id',$ctm_id)->find();
        if($MedicalRow && $MedicalRow['type']==2){
            return Utils::ajaxReturn(40007, '您已经申请过了', null);
        }
        if (empty($job_img)) {
            return Utils::ajaxReturn(40003, '请上传场景照', null);
        }  else {//上传图片
            $job_img = '';
            foreach ($job_imgarray as $k=>$v){
                $IndexPath = config('appconfig.IndexPath');
                $postFields['modeName'] = 'staff';
                $postFields['img'] = $job_imgarray[$k];
                $img = Utils::curlPost1($IndexPath . 'Public/uploadBase64Img/', $postFields);
                $img = json_decode($img,TRUE);
                $job_img = $job_img.','.$img['data'];
            }
            
            $job_img = substr($job_img,1);
        }
        if (empty($papers_img)) {
            //return Utils::ajaxReturn(40003, '请上传证件照', null);
            $papers_img = '';
        }  else {//上传图片
            $papers_img = '';
            foreach ($papers_imgarray as $k=>$v){
                $IndexPath = config('appconfig.IndexPath');
                $postFields['modeName'] = 'staff';
                $postFields['img'] = $papers_imgarray[$k];
                $img = Utils::curlPost1($IndexPath . 'Public/uploadBase64Img/', $postFields);
                $img = json_decode($img,TRUE);
                $papers_img = $papers_img.','.$img['data'];
            }
            
            $papers_img = substr($papers_img,1);
        }
        if (empty($card_img)) {
            //return Utils::ajaxReturn(40003, '请上传手持身份证照', null);
            $card_img = '';
        }  else {//上传图片
            $card_img = '';
            foreach ($card_imgarray as $k=>$v){
                $IndexPath = config('appconfig.IndexPath');
                $postFields['modeName'] = 'staff';
                $postFields['img'] = $card_imgarray[$k];
                $img = Utils::curlPost1($IndexPath . 'Public/uploadBase64Img/', $postFields);
                $img = json_decode($img,TRUE);
                $card_img = $card_img.','.$img['data'];
            }
            
            $card_img = substr($card_img,1);
        }
        Db::startTrans();
        
        $insertMedical = Medical::insert([
            'ctm_id' => $ctm_id,
            'name' => $name,
            'work_unit' => $work_unit,
            'job' => $job,
            'job_img' => $job_img,
            'papers_img' => $papers_img,
            'card_img' => $card_img,
            'add_time' => date('Y-m-d H:i:s'),
            'invite_id' => $invite_id,
            'type' => 2,
        ]);
        
        $addressRow = MedicalAddress::where('ctm_id',$ctm_id)->find();
        if($addressRow){
            $addressResult=MedicalAddress::where('ctm_id',$ctm_id)->update([
                'name'=>$name,
                'phone'=>$phone,
                'currentcity'=>$currentcity,
                'status'=>$status,
                'address'=>$address,
                'add_time' => date('Y-m-d H:i:s'),
            ]);
        } else {
            $addressResult=MedicalAddress::insert([
                'name'=>$name,
                'phone'=>$phone,
                'currentcity'=>$currentcity,
                'status'=>$status,
                'address'=>$address,
                'ctm_id' => $ctm_id,
                'add_time' => date('Y-m-d H:i:s'),
            ]);
        }
        
        if ($insertMedical && $addressResult) {
            Db::commit();
            return Utils::ajaxReturn(0, '提交成功', null);
        } else {
            Db::rollback();
            return Utils::ajaxReturn(40008, '提交失败', null);
        }  
    }
    
    /*
     * 寻找逆行者
     * 日期 2020-02-17
     */

    public function seekMedical() {
        $rule = [
            'ctm_id' => 'require',
            'name' => 'require',
            'phone'=> 'require',
            'currentcity' => 'require',
            'status' => 'require',
            'address' => 'require',
        ];
        $msg = [
            'ctm_id.require' => '用户ID必须',
            'name.require' => '姓名必须',
            'phone.require' => '手机号必须',
            'currentcity.require' => '所在城市必须',
            'status.require' => '是否进店必须',
            'address.require' => '邮寄地址必须',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }

        $ctm_id = $all['ctm_id'];
        $name = $all['name'];
        $phone = $all['phone'];
        $currentcity = $all['currentcity'];
        $status = $all['status'];
        $address = $all['address'];
        
        $addressRow = MedicalAddress::where('ctm_id',$ctm_id)->find();
        if($addressRow){
            $addressResult=MedicalAddress::where('ctm_id',$ctm_id)->update([
                'name'=>$name,
                'phone'=>$phone,
                'currentcity'=>$currentcity,
                'status'=>$status,
                'address'=>$address,
                'add_time' => date('Y-m-d H:i:s'),
            ]);
        } else {
            $addressResult=MedicalAddress::insert([
                'name'=>$name,
                'phone'=>$phone,
                'currentcity'=>$currentcity,
                'status'=>$status,
                'address'=>$address,
                'ctm_id' => $ctm_id,
                'add_time' => date('Y-m-d H:i:s'),
            ]);
        }
        
        if ($addressResult) {
            return Utils::ajaxReturn(0, '提交成功', null);
        } else {
            return Utils::ajaxReturn(40008, '提交失败', null);
        }  
    }
    
    /*
     * 逆行者活动海报
     * 日期 2020-02-17
     */

    public function medicalInfo() {
        $rule = [
            'ctm_id' => 'require',
            'invite_id' => 'require',
        ];
        $msg = [
            'ctm_id.require' => '用户ID必须',
            'invite_id.require' => '邀请者ID必须',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }

        $ctm_id = $all['ctm_id'];
        $invite_id = $all['invite_id'];
        
        $addressRow = MedicalAddress::where('ctm_id',$ctm_id)->find();
        $medicalRow = Medical::where('ctm_id',$ctm_id)
                ->where('type',2)
                ->field('id,name,work_unit,job,job_img,papers_img,card_img,status,why')
                ->find();
        $job_img = $medicalRow['job_img'];
        $job_img = explode(",", $job_img);
        for($i=0;$i<count($job_img);$i++){
            $job_img[$i] = str_replace("http://47.104.83.107/","https://www.kkmimg.com/",$job_img[$i]);
        }
        if($medicalRow){
            $medicalRow['job_img'] = implode(',', $job_img);
        }
        
        $papers_img = $medicalRow['papers_img'];
        $papers_img = explode(",", $papers_img);
        for($i=0;$i<count($papers_img);$i++){
            $papers_img[$i] = str_replace("http://47.104.83.107/","https://www.kkmimg.com/",$papers_img[$i]);
        }
        if($medicalRow){
            $medicalRow['papers_img'] = implode(',', $papers_img);
        }
        
        $card_img = $medicalRow['card_img'];
        $card_img = explode(",", $card_img);
        for($i=0;$i<count($card_img);$i++){
            $card_img[$i] = str_replace("http://47.104.83.107/","https://www.kkmimg.com/",$card_img[$i]);
        }
        if($medicalRow){
            $medicalRow['card_img'] = implode(',', $card_img);
        }
        
        $medicalRow2 = Medical::where('invite_id',$ctm_id)->select();
        if ($medicalRow2 && $ctm_id!=0) {
            foreach ($medicalRow2 as $k => $v) {
                $customRow = Custom::where('id', '=', $v['ctm_id'])
                        ->field('id,custom_name,custom_photo')
                        ->find();
                $customRow['add_time'] = $v['add_time'];
                $customRow['audit_time'] = $v['audit_time'];
                $customRow['status'] = $v['status'];
                $ctmdata[$k] = $customRow; 
            }
        }else{
            $ctmdata = [];
        }
        //邀请者信息
        $inviteCustom = Custom::where('id', '=', $invite_id)
                        ->field('id,custom_name,custom_photo')
                        ->find();
//        $data['addressRow'] = $addressRow;
//        $data['medicalRow'] = $medicalRow;
//        $data['ctmdata'] = $ctmdata;
//        $data['inviteCustom'] = $inviteCustom;
        
        $data['addressRow'] = [];
        $data['medicalRow'] = [];
        $data['ctmdata'] = [];
        $data['inviteCustom'] = [];
        
        return Utils::ajaxReturn(0, '查询成功', $data);
    }
    
    /*
     * 我的-逆行者活动
     * 日期 2020-02-26
     */

    public function medicalStatus() {
        $rule = [
            'ctm_id' => 'require',
        ];
        $msg = [
            'ctm_id.require' => '用户ID必须',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }

        $ctm_id = $all['ctm_id'];
        if(time() > 1593532800){
            $data['status'] = 5;
            return Utils::ajaxReturn(0, '查询成功', $data);
        }
        
        $medicalRow = Medical::where('ctm_id',$ctm_id)
                ->where('type',2)
                ->field('id,status')
                ->find();
        if($medicalRow){
            $status = $medicalRow['status'];
            if($status == 0 || $status == 2){
               $data['status'] = 1;//1我是逆行者（待审核）2我是逆行者（审核通过）3寻找逆行者（待审核）4寻找逆行者（审核成功）5没有参加活动 6我是逆行者（审核失败）7寻找逆行者（寻找中）8.寻找逆行者（审核失败）
            }elseif ($status == 1) {
                $data['status'] = 2;
            }elseif ($status == 3) {
                $data['status'] = 6;
            }
        } else {
            $medicalRow2 = Medical::where('invite_id',$ctm_id)
                    ->where('status',1)
                    ->find();
            if($medicalRow2){
                $data['status'] = 4;
            } else {
                $medicalRow3 = Medical::where('invite_id',$ctm_id)
                    ->where('status',3)
                    ->find();
                if($medicalRow3){
                    $data['status'] = 8;
                } else {
                    $medicalRow4 = Medical::where('invite_id',$ctm_id)
                        ->where('status','in','0,2')
                        ->find();  
                    if($medicalRow4){
                        $data['status'] = 3;
                    } else {
                        $address = MedicalAddress::where('ctm_id',$ctm_id)->find();
                        if($address){
                            $data['status'] = 7;
                        } else {
                            $data['status'] = 5;
                        }
                    }
                }
            }
        }
        return Utils::ajaxReturn(0, '查询成功', $data);
    }
    
    /*
     * 白衣天使审核失败重新报名
     * 日期 2020-02-17
     */

    public function againMedicalEnroll() {
        $rule = [
            'ctm_id' => 'require',
            'name' => 'require',
            'work_unit' => 'require',
            'job' => 'require',
            'phone'=> 'require',
            'currentcity' => 'require',
            'status' => 'require',
            'address' => 'require',
        ];
        $msg = [
            'ctm_id.require' => '用户ID必须',
            'name.require' => '姓名必须',
            'work_unit.require' => '工作单位必须',
            'job.require' => '职务必须',
            'phone.require' => '手机号必须',
            'currentcity.require' => '所在城市必须',
            'status.require' => '是否进店必须',
            'address.require' => '邮寄地址必须', 
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }
        $ctm_id = $all['ctm_id'];
        $name = $all['name'];
        $work_unit = $all['work_unit'];
        $job = $all['job'];
        $phone = $all['phone'];
        $currentcity = $all['currentcity'];
        $status = $all['status'];
        $address = $all['address'];
        $job_img = input('param.job_img/a'); //工作证
        $job_imgarray = $job_img;
        $papers_img = input('param.papers_img/a'); //证件照
        $papers_imgarray = $papers_img;
        $card_img = input('param.card_img/a'); //手持身份证
        $card_imgarray = $card_img;
        //$job_imgarray = explode(",", $job_img);
        $ctmRow = Custom::where('id', $ctm_id)->find();
//        if($ctmRow['vip_status'] == 2){
//            return Utils::ajaxReturn(40004, '您已是特权用户', null);
//        }
        $MedicalRow = Medical::where('ctm_id',$ctm_id)->where('type',2)->find();
        $id = $MedicalRow['id'];
        if (empty($job_img)) {
            $job_img = $MedicalRow['job_img'];
        }  else {//上传图片
            $job_img = '';
            foreach ($job_imgarray as $k=>$v){
                $IndexPath = config('appconfig.IndexPath');
                $postFields['modeName'] = 'staff';
                $postFields['img'] = $job_imgarray[$k];
                $img = Utils::curlPost1($IndexPath . 'Public/uploadBase64Img/', $postFields);
                $img = json_decode($img,TRUE);
                $job_img = $job_img.','.$img['data'];
            }
            
            $job_img = substr($job_img,1);
        }
        if (empty($papers_img)) {
            $papers_img = $MedicalRow['papers_img'];
        }  else {//上传图片
            $papers_img = '';
            foreach ($papers_imgarray as $k=>$v){
                $IndexPath = config('appconfig.IndexPath');
                $postFields['modeName'] = 'staff';
                $postFields['img'] = $papers_imgarray[$k];
                $img = Utils::curlPost1($IndexPath . 'Public/uploadBase64Img/', $postFields);
                $img = json_decode($img,TRUE);
                $papers_img = $papers_img.','.$img['data'];
            }
            
            $papers_img = substr($papers_img,1);
        }
        
        if (empty($card_img)) {
            $card_img = $MedicalRow['card_img'];
        }  else {//上传图片
            $card_img = '';
            foreach ($card_imgarray as $k=>$v){
                $IndexPath = config('appconfig.IndexPath');
                $postFields['modeName'] = 'staff';
                $postFields['img'] = $card_imgarray[$k];
                $img = Utils::curlPost1($IndexPath . 'Public/uploadBase64Img/', $postFields);
                $img = json_decode($img,TRUE);
                $card_img = $card_img.','.$img['data'];
            }
            
            $card_img = substr($card_img,1);
        }
        Db::startTrans();
        
        $insertMedical = Medical::where('id',$id)->update([
            'ctm_id' => $ctm_id,
            'name' => $name,
            'work_unit' => $work_unit,
            'job' => $job,
            'job_img' => $job_img,
            'papers_img' => $papers_img,
            'card_img' => $card_img,
            'add_time' => date('Y-m-d H:i:s'),
            'type' => 2,
            'status' => 0,
        ]);
        
        $addressResult=MedicalAddress::where('ctm_id',$ctm_id)->update([
            'name'=>$name,
            'phone'=>$phone,
            'currentcity'=>$currentcity,
            'status'=>$status,
            'address'=>$address,
            'add_time' => date('Y-m-d H:i:s'),
        ]);
        
        if ($insertMedical && $addressResult) {
            Db::commit();
            return Utils::ajaxReturn(0, '提交成功', null);
        } else {
            Db::rollback();
            return Utils::ajaxReturn(40008, '提交失败', null);
        }  
    }
    
    /*
     * 我的-逆行者活动详情
     * 日期 2020-03-07
     */

    public function myMedicalInfo() {
        $rule = [
            'ctm_id' => 'require',
        ];
        $msg = [
            'ctm_id.require' => '用户ID必须',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }

        $ctm_id = $all['ctm_id'];
        
        $addressRow = MedicalAddress::where('ctm_id',$ctm_id)->find();
        $medicalRow = Medical::where('ctm_id',$ctm_id)
                ->where('type',2)
                ->field('id,name,work_unit,job,job_img,papers_img,card_img,status,why')
                ->find();
        if($medicalRow['papers_img']==null){
            $medicalRow['papers_img'] = '';
        }
        if($medicalRow['card_img']==null){
            $medicalRow['card_img'] = '';
        }
        $data['addressRow'] = $addressRow;
        $data['medicalRow'] = $medicalRow;
        
        return Utils::ajaxReturn(0, '查询成功', $data);
    }
    
    /*
     * 我的-逆行者获奖名单
     * 日期 2020-03-08
     */

    public function medicalWinList() {
        $medicallist = Medical::alias('m')
                ->join('custom c','c.id = m.ctm_id','left')
                ->where('m.status',1)
                ->where('m.type',2)
                ->limit(0,100)
                ->order('m.add_time desc')
                ->field('c.custom_name,c.custom_photo')
                ->select();
        $num = count($medicallist);
        foreach ($medicallist as $k=>$v){
            $medicallist[$k]['content'] = '刚刚成功参加“寻找逆行者”活动，已免费开通特权';
            //$string = iconv('utf-8','gbk',$v['custom_name']);
//            $string = $v['custom_name'];
//            $substr = substr($string,0,2);
//            $str = iconv('gbk','utf-8',$substr);
            
            
            $str = $v['custom_name'];
            //$str = substr($string,0,2);
//            $str = iconv('gbk','utf-8',$substr);
            
            $medicallist[$k]['custom_name'] = $str.'****';
//            $medicallist[$num+$k]['custom_name'] = $str.'****';
//            $medicallist[$num+$k]['custom_photo'] = $v['custom_photo'];
//            $medicallist[$num+$k]['content'] = '刚刚成功参加“寻找逆行者”活动，已免费开通特权';
        }
        
//        $random = $this->shuffle_assoc($medicallist);
//        $random = array_values($random);
        if(time() > 1593532800){
            $medicallist = [];
        }
        return Utils::ajaxReturn(0, '查询成功', $medicallist);
    }
    
    public function shuffle_assoc($list) {  
        if (!is_array($list)){
            return $list; 
        } 
        $keys = array_keys($list);  
        shuffle($keys);  
        $random = array();  
        foreach ($keys as $key){
            $random[$key] = $this->shuffle_assoc($list[$key]); 
        }  
        return $random;  
     }
    

}
